package com.chinacoal.microservice.impl.controller;


import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.api.TaskCatRegisterClient;
import com.chinacoal.microservice.api.model.QueryUtaskCatParam;
import com.chinacoal.microservice.api.model.entity.UtaskCatEntity;
import com.chinacoal.microservice.impl.service.UtaskCatService;
import com.chinacoal.microservice.login.LoginUser;
import com.chinacoal.microservice.model.IamUser;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;

@RestController
public class UtaskCatController implements TaskCatRegisterClient {

    @Resource
    private UtaskCatService utaskCatService;
    @Resource
    private LoginUser loginUser;
    
	@Override
	public Result<Object> addTaskReg(UtaskCatEntity utaskCatEntity) {
    	try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskCatEntity.setCreationDate(new Date());
			utaskCatEntity.setCreatedBy(iamUser.getUserId());
			utaskCatEntity.setCreatedByName(iamUser.getName());
    		
			// 执行新增
			utaskCatService.addTaskReg(utaskCatEntity);
			
			return Result.success("新增待办类型成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> updateTaskReg(UtaskCatEntity utaskCatEntity) {
		try {
    		IamUser iamUser = loginUser.getLoginUser();
			if(iamUser == null) throw new Exception("用户未登录");
    		
			// 数据补全
			utaskCatEntity.setLastUpdateDate(new Date());
			utaskCatEntity.setLastUpdatedBy(iamUser.getUserId());
			utaskCatEntity.setLastUpdatedByName(iamUser.getName());
    		
			// 执行更新
			utaskCatService.updateTaskReg(utaskCatEntity);
			
			return Result.success("更新待办类型成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Object> deleteTaskReg(String catId) {
		try {
			// 执行删除
			utaskCatService.deleteTaskReg(catId);
			return Result.success("删除待办类型成功");   
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<Page<UtaskCatEntity>> getList(Integer page, Integer pageSize, QueryUtaskCatParam param) {
		try {
    		Page<UtaskCatEntity> utaskCatEntityPage = utaskCatService.getList(page, pageSize, param);
			return Result.success(utaskCatEntityPage);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<UtaskCatEntity> getTaskCat(String catId) {
		try {
			UtaskCatEntity utaskCatEntity = utaskCatService.selectById(catId);
			return Result.success(utaskCatEntity);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<List<UtaskCatEntity>> getTaskCatBySysId(String sysId) {
		try {
			List<UtaskCatEntity> utaskCatEntityList = utaskCatService.getTaskCatBySysId(sysId);
			return Result.success(utaskCatEntityList);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

	@Override
	public Result<List<UtaskCatEntity>> getLowerTaskCat(String upCatId) {
		try {
			List<UtaskCatEntity> utaskCatEntityList = utaskCatService.getLowerTaskCat(upCatId);
			return Result.success(utaskCatEntityList);
    	}catch (Exception e) {
    		e.printStackTrace();
    		return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
		}
	}

}

